# List All IIS SSL Sites
Import-Module WebAdministration

try {
    $sites = @()

    Get-Website | ForEach-Object {
        $site = $_
        $httpsBindings = $site.Bindings.Collection | Where-Object { $_.protocol -eq "https" }

        foreach ($binding in $httpsBindings) {
            $certHash = $binding.certificateHash
            $cert = $null

            if ($certHash) {
                $cert = Get-ChildItem -Path Cert:\LocalMachine\My | Where-Object { $_.Thumbprint -eq $certHash }
            }

            $siteInfo = [PSCustomObject]@{
                SiteName = $site.Name
                State = $site.State
                HostName = $binding.bindingInformation.Split(':')[2]
                Port = [int]$binding.bindingInformation.Split(':')[1]
                IP = $binding.bindingInformation.Split(':')[0]
                CertThumbprint = $certHash
                CertSubject = if ($cert) { $cert.Subject } else { "N/A" }
                CertIssuer = if ($cert) { $cert.Issuer } else { "N/A" }
                CertExpires = if ($cert) { $cert.NotAfter.ToString("yyyy-MM-dd HH:mm:ss") } else { "N/A" }
                CertDaysLeft = if ($cert) { [int]($cert.NotAfter - (Get-Date)).TotalDays } else { -1 }
            }

            $sites += $siteInfo
        }
    }

    # 输出为 JSON
    $sites | ConvertTo-Json -Depth 10
    exit 0

} catch {
    Write-Error $_.Exception.Message
    exit 1
}
